package com.limao.CacheManager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Set;

import com.Hazelcast.Cache;
import com.Hazelcast.CacheFactory;
import com.limao.DBDataClass.HomeColumClass;
import com.limao.DBProvider.HomeMenuProvider;
import com.limao.Tool.CacheClass;
import com.limao.cache.BasicModule;

public class HomeColumManager extends BasicModule{
	
	private HomeMenuProvider provider;
	private static HomeColumManager instance;

    public static HomeColumManager getInstance() {
        return instance;
    }
 
    private Cache<String, HomeColumClass> homeMenuDataCache;

    public HomeColumManager() {
    	super("homeMenuManager");
    	homeMenuDataCache = CacheFactory.createCache("cd_homeMenuDataCache");
    	
    	initProvider();
    }
    
    
    @Override
	public void initialize() {
        super.initialize();
        instance=this;
    }
    

	
    public void ClearObjectCache(String key)
    {
    	homeMenuDataCache.remove(key);
    }  
    
    public ArrayList<HomeColumClass> Load_HomeColum_All()
    {
    	ArrayList<HomeColumClass> list=new ArrayList<HomeColumClass>();
        Set set =homeMenuDataCache.keySet();
      	Iterator it=set.iterator();
      	while(it.hasNext()){
       		String s= (String) it.next();
      		list.add(homeMenuDataCache.get(s));
      	}
      	
      	if(list.size()==0)
    	{
      		list=provider.Load_HomeColum_All();
	    	for (HomeColumClass obj : list) 
	    	{
	    		homeMenuDataCache.put(obj.columid, obj);
	     	}
    	}
      	else
      	{
        	Collections.sort(list, new Comparator<HomeColumClass>() {
        		public int compare(HomeColumClass arg0, HomeColumClass arg1) {
        				return ((Integer)CacheClass.getIntValue(arg0.sortvalue)).compareTo(CacheClass.getIntValue(arg1.sortvalue));
        		}
        	});
      	}
    	
    	return list;
    }

    public HomeColumClass Load_HomeColum_byDefault()
    {
    	HomeColumClass tmp=null;
    	ArrayList<HomeColumClass> list=Load_HomeColum_All();
    	for (HomeColumClass obj : list) 
    	{
    		if("1".equalsIgnoreCase(obj.isdefault))
    		{
    			tmp=obj;
    		}
     	}
    	
    	return tmp;
    }

	public HomeColumClass Load_HomeColum_byColumid(String  Columid)
    {
    	if(CacheClass.isEmpty(Columid)) 
    		return null;
    	HomeColumClass obj=homeMenuDataCache.get(Columid);
    	
    	if (obj == null) {
            synchronized (Columid.intern()) {
            	
            	obj=homeMenuDataCache.get(Columid);
                
                if (obj == null) {
                	//取出全部参数
                	obj=provider.Load_HomeColum_byColumid(Columid);
					if(obj!=null)
					{
						homeMenuDataCache.put(obj.columid, obj);
					}
                }
            }
    	}
	 
    	return obj;
    }
    
   
    
//******************************************************
    private void initProvider() {
        if (provider == null ) {
            try {
                provider =HomeMenuProvider.getInstance();
            }
            catch (Exception e) {
            	e.printStackTrace();
            }
        }
    }
  
}

